<template>
	<view class="app l_pages">
		<jnavbar :title="'核销'"></jnavbar>
		<scroll-view class="page_container" scroll-y>
			<view class="bg_img"><vimage mode="widthFix" class="" imgtype="1" src="static/images/hexiao.png"></vimage></view>
			<view class="title">扫描券码 一键核销</view>
			<view class="tip">降低核销门槛 低成本留存优质客户</view>
			<view class="btns">
				<u-button :custom-style="customStyle" @click="saoma">扫码核销</u-button>
				<u-button :custom-style="customStyle1" @click="tolist">核销记录</u-button>
			</view>
		</scroll-view>
		<uni-login ref="Login" @callback="logincallback"></uni-login>
		<tab-bar ref="tabbar"></tab-bar>
	</view>
</template>

<script>
import {mapState, mapMutations} from 'vuex';
import {scanQRCode} from '@/common/wxJsSDK';
import util from '@/common/util.js';

export default {
	onShareAppMessage() {
		return this.wxShare();
	},
	data() {
		return {
			customStyle: {
				width: '318rpx',
				height: '90rpx',
				border: '2rpx solid #ff4206',
				'border-radius': '10rpx',
				'font-size': '34rpx',
				'font-weight': 'bold',
				background: '#f4f4f4',
				color: '#ff4206'
			},
			customStyle1: {
				width: '318rpx',
				height: '90rpx',
				'border-radius': '10rpx',
				'font-size': '34rpx',
				'font-weight': 'bold',
				background: 'linear-gradient(90deg, #fe8e0a, #ff5011)',
				color: '#ffffff',
				border: 'none'
			}
		};
	},
	computed: {
		...mapState({
			hastabbar: state => state.hastabbar
		})
	},
	mounted() {
		var _this = this;
		this.$nextTick(a => {
			_this.$refs.tabbar.gettabs();
		});
	},
	methods: {
		logincallback() {},
		toaccept(data, type) {
			if (type == 1) {
				this.Jumpurl({
					link: `../accept/accept?order=${data.order}&ids=${data.ids}&store=${data.store}&user=${data.user}`,
					type: 1,
					login_type: 2
				});
			} else if (type == 2) {
				this.Jumpurl({
					link: `../accept/accept?a=${data.a}&ids=${data.ids}&stoer_id=${data.store_id}`,
					type: 1,
					login_type: 2
				});
			}
		},
		getwxaQr(wxa) {
			this.Jumpurl({
				link: `../accept/accept?scene=${wxa}`,
				type: 1,
				login_type: 2
			});
		},
		analysis(result){
			if (!result) {
				uni.showToast({
					title: '无效二维码',
					icon: 'none'
				});
			}
			let a = util.getQueryVariable('a', result);
			let order = util.getQueryVariable('order', result);
			let ids = util.getQueryVariable('ids', result);
			let store = util.getQueryVariable('store', result);
			let user = util.getQueryVariable('user', result);
			let store_id = util.getQueryVariable('store_id', result);
			
			if (ids) {
				ids = decodeURIComponent(ids);
			}
			if (a && ids) {
				this.toaccept({a: a, ids: ids, store: store_id}, 2);
			}
			if (order && ids && store && user) {
				let data = {
					order: order,
					ids: ids,
					store: store,
					user: user
				};
			
				this.toaccept(data, 1);
			} else {
				uni.showToast({
					title: '无效二维码',
					icon: 'none'
				});
			}
		},
		saoma() {
			var _this = this;
			// #ifdef H5
			scanQRCode(res => {
				_this.analysis(res.resultStr)
			}, 1);
			// #endif

			// #ifdef MP-WEIXIN

			uni.scanCode({
				onlyFromCamera: true,
				success: function(res) {
					// // console.log('条码类型：' + res.scanType);
					console.log('条码内容：' + res);
					if (res.scanType == 'WX_CODE') {
						let result = res.path;
						if (!result) {
							uni.showToast({
								title: '无效二维码',
								icon: 'none'
							});
						}
						let wxa = util.getQueryVariable('scene', result);

						if (wxa) {
							_this.getwxaQr(wxa);
						} else {
							uni.showToast({
								title: '无效二维码',
								icon: 'none'
							});
						}
					} else {
						_this.analysis(res.result)
					}
				},
				fail(e) {
					uni.showToast({
						title: '无效二维码',
						icon: 'none'
					});
				}
			});
			// #endif
		},
		tolist() {
			this.Jumpurl({
				link: '../accept/list',
				type: 1,
				login_type: 2
			});
		}
	}
};
</script>

<style lang="scss">
.bg_img {
	width: 567rpx;
	margin: 100rpx 95rpx 150rpx 88rpx;
}
.title {
	text-align: center;
	font-size: 48rpx;
	font-weight: bold;
	color: #101534;
	line-height: 45rpx;
}
.tip {
	text-align: center;
	font-size: 32rpx;
	font-weight: bold;
	color: rgba(37, 90, 124, 0.46);
	line-height: 45rpx;
	margin-top: 30rpx;
}
.btns {
	display: flex;
	position: absolute;
	bottom: 60rpx;
	width: 100%;
	padding: 0 46rpx;
	justify-content: space-between;
	.btn {
		width: 318rpx;
		height: 90rpx;
		border: 2rpx solid #ff4206;
		border-radius: 10rpx;
		font-size: 34rpx;
		font-weight: bold;
		background: #f4f4f4;
		color: #ff4206;

		&.sure {
			background: linear-gradient(90deg, #fe8e0a, #ff5011);
			color: #ffffff;
			border: none;
		}
	}
}
</style>
